package cn.buk.tms.eotms.config;

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import jakarta.persistence.EntityManagerFactory;

import javax.sql.DataSource;

import static org.springframework.orm.jpa.vendor.Database.MYSQL;

/**
 * Created by yfdai on 2016/12/5.
 */
@Profile({"dev","demo"})
@Configuration
@EnableTransactionManagement
public class DataSourceConfigurationDev {

    /**
     * read config data from application.yml
     * @return
     */
    @Bean(name = "mainDataSource")
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource dataSource() {
//        return DataSourceBuilder.create().build();
        return DataSourceBuilder.create().type(HikariDataSource.class).build();
    }


    @Bean
    public JpaVendorAdapter jpaVendorAdapter() {
        HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
        adapter.setDatabase(MYSQL);
        adapter.setShowSql(false);
        adapter.setGenerateDdl(true);
//        adapter.setDatabasePlatform("org.hibernate.dialect.MySQL57Dialect");
//        adapter.setDatabasePlatform("org.hibernate.dialect.MySQL57Dialect");

        adapter.getJpaPropertyMap().put("hibernate.dialect.storage_engine", "innodb");
//        adapter.getJpaPropertyMap().put("hibernate.id.new_generator_mappings", "false"); // 5.2的主键生成策略默认的不是GenerationType.IDENTITY
        return adapter;
    }


    @Bean
//    @Autowired
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource mainDataSource, JpaVendorAdapter jpaVendorAdapter) {
        LocalContainerEntityManagerFactoryBean emfb = new LocalContainerEntityManagerFactoryBean();

        emfb.setDataSource(mainDataSource);
        emfb.setJpaVendorAdapter(jpaVendorAdapter);
//        emfb.setPersistenceUnitName("qms");

        emfb.setPackagesToScan("cn.buk.tms.eotms.entity");

        return emfb;
    }


    @Bean
    public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory){
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(entityManagerFactory);

        return transactionManager;
    }


    @Bean
    public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){
        return new PersistenceExceptionTranslationPostProcessor();
    }
}
